<?php


namespace app\controller\index;
require_once __DIR__.'/../../common/AuthController.php';
use app\common\AuthController;


/**
 * 后台首页
 */
class Index extends AuthController
{   


    /**
     * 初始化操作
     */
    public function init()
    {

    }
	

    /**
     * 后台首页信息
     */
	public function api_index_index_await($msg)
    {   
        global $db;
        $equipment_count = $this->table->getCount('equipment_request',['status' => 1]);
        $video_count = $this->table->getCount('ad_video',['check_status' => 1]);
        $maintain_count = $this->table->getCount('equipment_malfunction_bills',['status' => 1]);
        $supply_count = $db->query("SELECT count(*) as count from equipment_goods where surplus_number < threshold");
        $start = date('Y-m-d',time()).' 00:00:00';
        $today_order_count = $db->query("SELECT count(*) as count,sum(order_money) as  today_order_money from `order` where order_status = 2 AND pay_time > '".$start."'");
        $all_order_count = $db->query("SELECT count(*) as count,sum(order_money) as  all_order_money from `order`");
        $data = [
            'equipment_count' => $equipment_count[0]['count'],
            'video_count' => $video_count[0]['count'],
            'maintain_count' => $maintain_count[0]['count'],
            'supply_count' => $supply_count[0]['count'],
            'today_order_count' => $today_order_count[0]['count'],
            'today_order_money' => $today_order_count[0]['today_order_money'],
            'all_order_count' => $all_order_count[0]['count'],
            'all_order_money' => $all_order_count[0]['all_order_money']
        ];
        return $this->outMessage(10000,'获取成功',$data);
	}



    /**
     * 本月终端销售top10
     */
    public function api_index_index_sellRanking($msg)
    {   
        global $db;
        $start = date('Y-m',time()).'-01 00:00:00';
        $equipment_count = $db->query("SELECT count(*) as count,equipment_name  FROM `order` WHERE create_time > '".$start."' GROUP BY equipment_id ORDER BY count desc limit 10");
        $goods_count = $db->query("SELECT sum(`number`) as count,goods_name  FROM order_goods WHERE create_time > '".$start."' GROUP BY goods_id ORDER BY count desc limit 10");
        $data = [
            'equipment_count' => $equipment_count,
            'goods_count' => $goods_count
        ];
        return $this->outMessage(10000,'获取成功',$data);
    }



    /**
     * 终端在线数量及排行榜
     */
    public function api_index_index_regionRanking($msg)
    {   
        global $db;
        $sum_count = $this->table->getCount('equipment_list',['status'=>2]);
        $region_equipment_ranking = $db->query("SELECT count(*) as count,region_name,region_id  FROM equipment_list WHERE status = 2 GROUP BY region_id ORDER BY count desc");
        $data = [
            'sum_count' => $sum_count[0]['count'],
            'region_equipment_ranking' => $region_equipment_ranking
        ];
        return $this->outMessage(10000,'获取成功',$data);
    }



    /**
     * 获取支付方式比例
     */
    public function api_index_index_payData($msg)
    {
        global $db;
        $start = date('Y-m',time()).'-01 00:00:00';
        $ratio = $db->query("SELECT count(*) as count FROM `order` WHERE order_status = 2 AND create_time > '".$start."'");
        $wxpay = $db->query("SELECT count(*) as count FROM `order` WHERE order_status = 2 AND pay_type = 1 AND create_time > '".$start."'");
        $alipay = $db->query("SELECT count(*) as count FROM `order` WHERE order_status = 2 AND pay_type = 2 AND create_time > '".$start."'");
        $data = [
            'wxpay' => $wxpay[0]['count']/$ratio[0]['count']*100,
            'alipay' => $alipay[0]['count']/$ratio[0]['count']*100
        ];
        return $this->outMessage(10000,'获取成功',$data);
    }



    /**
     * 获取前6个月的销售额
     */
    public function api_index_index_saleData($msg)
    {
        global $db;
        $currentTime = time();
        $cyear = floor(date("Y",$currentTime));
        $cMonth = floor(date("m",$currentTime));
        $list = [];
        for($i=0;$i<6;$i++){
            $nMonth = $cMonth-$i;
            $cyear = $nMonth == 0 ? ($cyear-1) : $cyear;
            $nMonth = $nMonth <= 0 ? 12+$nMonth : $nMonth;
            $date = $cyear."-".$nMonth."-1";
            $firstday = date('Y-m-01', strtotime($date))." 00:00:00";
            $lastday = date('Y-m-t', strtotime($date))." 23:59:59";
            $data = $db->query("SELECT sum(order_money) as count FROM `order` WHERE order_status = 2 AND create_time > '".$firstday."' AND create_time < '".$lastday."'");
            $list[$i]['month'] = $nMonth;
            $list[$i]['sum_money'] = !empty($data[0]['count'])?$data[0]['count']:0;
        }
        return $this->outMessage(10000,'获取成功',$list);
    }

}
